package com.beauty.product.dto;

import com.beauty.product.entity.Product;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductDto {
    
    private String id;
    
    @NotBlank(message = "商品名称不能为空")
    private String name;
    
    @NotBlank(message = "商品SKU不能为空")
    private String sku;
    
    @NotNull(message = "商品价格不能为空")
    @Positive(message = "商品价格必须大于0")
    private BigDecimal price;
    
    private BigDecimal originalPrice;
    
    @NotNull(message = "商品类别不能为空")
    private Long categoryId;
    
    private String categoryName;
    
    private String brand;
    
    private String description;
    
    private String coverImage;
    
    private Boolean isPublished = false;
    
    private Boolean isRecommended = false;
    
    private Boolean isNew = false;
    
    private Boolean isHot = false;
    
    private Integer sort = 0;
    
    private Integer sales = 0;
    
    private LocalDateTime createdTime;
    
    private LocalDateTime updatedTime;
    
    private List<ProductImageDto> images;
    
    private List<ProductAttributeDto> attributes;
    
    public static ProductDto fromEntity(Product product) {
        ProductDto dto = new ProductDto();
        dto.setId(product.getId());
        dto.setName(product.getName());
        dto.setSku(product.getSku());
        dto.setPrice(product.getPrice());
        dto.setOriginalPrice(product.getOriginalPrice());
        dto.setCategoryId(product.getCategoryId());
        dto.setBrand(product.getBrand());
        dto.setDescription(product.getDescription());
        dto.setCoverImage(product.getCoverImage());
        dto.setIsPublished(product.getIsPublished());
        dto.setIsRecommended(product.getIsRecommended());
        dto.setIsNew(product.getIsNew());
        dto.setIsHot(product.getIsHot());
        dto.setSort(product.getSort());
        dto.setSales(product.getSales());
        dto.setCreatedTime(product.getCreatedTime());
        dto.setUpdatedTime(product.getUpdatedTime());
        return dto;
    }
    
    public Product toEntity() {
        Product product = new Product();
        product.setId(this.id);
        product.setName(this.name);
        product.setSku(this.sku);
        product.setPrice(this.price);
        product.setOriginalPrice(this.originalPrice);
        product.setCategoryId(this.categoryId);
        product.setBrand(this.brand);
        product.setDescription(this.description);
        product.setCoverImage(this.coverImage);
        product.setIsPublished(this.isPublished);
        product.setIsRecommended(this.isRecommended);
        product.setIsNew(this.isNew);
        product.setIsHot(this.isHot);
        product.setSort(this.sort);
        product.setSales(this.sales != null ? this.sales : 0);
        if (this.createdTime == null) {
            product.setCreatedTime(LocalDateTime.now());
        } else {
            product.setCreatedTime(this.createdTime);
        }
        product.setUpdatedTime(LocalDateTime.now());
        return product;
    }
}